package com.ffggffgfv.backend.controller;

import com.ffggffgfv.backend.dto.MerchantLoginDTO;
import com.ffggffgfv.backend.dto.MerchantRegisterDTO;
import com.ffggffgfv.backend.entity.Merchant;
import com.ffggffgfv.backend.service.MerchantService;
import com.ffggffgfv.backend.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 商家认证控制器
 */
@RestController
@RequestMapping("/api/merchant/auth")
public class MerchantAuthController {

    @Autowired
    private MerchantService merchantService;

    /**
     * 商家登录
     *
     * @param loginDTO 登录信息
     * @return 登录结果
     */
    @PostMapping("/login")
    public Result login(@RequestBody MerchantLoginDTO loginDTO) {
        // 参数验证
        if (loginDTO.getUsername() == null || loginDTO.getUsername().trim().isEmpty()) {
            return Result.validateFailed("用户名不能为空");
        }
        if (loginDTO.getPassword() == null || loginDTO.getPassword().trim().isEmpty()) {
            return Result.validateFailed("密码不能为空");
        }

        // 执行登录
        Map<String, Object> loginResult = merchantService.login(loginDTO);

        if (loginResult == null) {
            return Result.validateFailed("用户名或密码错误");
        }

        return Result.success(loginResult, "登录成功");
    }

    /**
     * 商家注册
     *
     * @param registerDTO 注册信息
     * @return 注册结果
     */
    @PostMapping("/register")
    public Result register(@RequestBody MerchantRegisterDTO registerDTO) {
        // 参数验证
        if (registerDTO.getUsername() == null || registerDTO.getUsername().trim().isEmpty()) {
            return Result.validateFailed("用户名不能为空");
        }

        if (registerDTO.getPassword() == null || registerDTO.getPassword().trim().isEmpty()) {
            return Result.validateFailed("密码不能为空");
        }

        if (!registerDTO.getPassword().equals(registerDTO.getConfirmPassword())) {
            return Result.validateFailed("两次输入密码不一致");
        }

        if (registerDTO.getName() == null || registerDTO.getName().trim().isEmpty()) {
            return Result.validateFailed("商家名称不能为空");
        }

        if (registerDTO.getContact() == null || registerDTO.getContact().trim().isEmpty()) {
            return Result.validateFailed("联系人不能为空");
        }

        if (registerDTO.getPhone() == null || registerDTO.getPhone().trim().isEmpty()) {
            return Result.validateFailed("联系电话不能为空");
        }

        if (registerDTO.getAddress() == null || registerDTO.getAddress().trim().isEmpty()) {
            return Result.validateFailed("地址不能为空");
        }

        // 检查用户名是否已存在
        if (merchantService.isUsernameExists(registerDTO.getUsername())) {
            return Result.conflict("用户名已存在");
        }

        // 检查手机号是否已存在
        if (merchantService.isPhoneExists(registerDTO.getPhone())) {
            return Result.conflict("手机号已被注册");
        }

        // 注册商家
        try {
            Merchant merchant = merchantService.register(registerDTO);

            // 隐藏密码
            merchant.setPassword(null);

            return Result.success(merchant, "商家注册成功，请等待管理员审核");
        } catch (Exception e) {
            return Result.failed("注册失败：" + e.getMessage());
        }
    }
}
